rm(list=ls())
library(tidyverse)
library(magrittr)
library(estimatr)
library(rddensity)
library(rdd)
library(lfe)
library(gt)
library(stargazer)
library(kableExtra)
library(gridExtra)
library(grid)
library(ggalt)
library(showtext)
library(foreign)
library(maptools)

setwd("~/Dropbox/Dissertation/BKN survey/_apsr_kuipers_replication_file/")

##-----------------------------------------------------------------------------------------------------------------------------------------
#Preliminaries------------------------------------------------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------------------------------------------------------

#load functions
source("./_2_code/__functions.R")

##cleaning up code
keep_objs = lsf.str() %>% as.vector()
keep_objs = append(keep_objs, c("data_1", "data_2", "data_3", "data_4", "data_5", "keep_objs"))

##-----------------------------------------------------------------------------------------------------------------------------------------
#DATA CLEANING------------------------------------------------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------------------------------------------------------
#wrangle administrative data
#source("./_2_code/1_make_applicant_data.R")

#select survey variables
#source("./_2_code/1_make_survey_data.R")

#merge data and make IPW weights
#source("./_2_code/1_make_weights_merge_select.R")

#recode variables and make indices
#source("./_2_code/1_make_analysis_data.R")

#clean up
#rm(list=setdiff(ls(), keep_objs)) 


##-----------------------------------------------------------------------------------------------------------------------------------------
#LOAD DATASETS-----------------------------------------------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------------------------------------------------------

data_1 <- read.csv("./_3_data/1_data.csv")
data_2 <- read.csv("./_3_data/2_data.csv")
data_3 <- read.csv("./_3_data/3_data.csv")
data_4 <- read.csv("./_3_data/4_data.csv")
data_5 <- read.csv("./_3_data/5_data.csv")


##-----------------------------------------------------------------------------------------------------------------------------------------
#MAIN PAPER ANALYSES-----------------------------------------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------------------------------------------------------

source("./_2_code/2_table_2.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/2_table_3.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/2_table_4.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/2_table_5.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/2_figure_3.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/2_figure_4.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

##-----------------------------------------------------------------------------------------------------------------------------------------
#APPENDIX ANALYSES-------------------------------------------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------------------------------------------------------------------
#not run -- replicators don't have access to this data
#source("./_2_code/3_figure_a1.R") 
#rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_figure_a2.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_figure_a3.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

#not run -- replicators don't have access to this data
#source("./_2_code/3_figure_a4.R") 
#rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_figure_a5.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_figure_a6.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_figure_a8.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_figure_a9.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a1.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a2.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

#not run -- replicators don't have access to this data
#source("./_2_code/3_table_a3.R") 
#rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a4.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a5.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a6.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a7.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a8.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a9.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a10.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a11_a12.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a13.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a14.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a15.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a16.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a17.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/3_table_a18.R") 
rm(list=setdiff(ls(), keep_objs)) #clean up
















